﻿using System;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class UserGroupController : Controller
    {
        [HttpGet]

        public ActionResult Index()
        {
            var model = new UserGroupIndexViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var page = proxy.Channel.GetUserGroupPaged(param.ToPagingFeature());

                return this.Store(page);
            }
        }


        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            var model = new UserGroupViewModel
            {

            };
            return View("Edit", model);
        }


        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var model = proxy.Channel.GetUserGroupByKey(id).To<UserGroupViewModel>();

                return View("Edit", model);
            }
        }


        [HttpPost]
        public ActionResult Save(UserGroupDTO vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result() {Err = "提交数据出错。"});
            }
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.SaveUserGroup(vm);
                res.RedirectToUrl = Url.Action("Index");

                return this.JsonNet(res);
            }
        }

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids, FormCollection collection)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.DeleteUserGroups(ids);
                return this.JsonNet(res);
            }
        }

        public ActionResult UserDialog(Guid userGroupid)
        {

            return View(userGroupid);
        }

        /// <summary>
        /// 关联用户
        /// </summary>
        /// <param name="userGroupId">用户组Id</param>
        /// <param name="userIds">用户Id</param>
        /// <returns></returns>
        public ActionResult Relation(Guid userGroupId, Guid[] userIds)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.RelationUser(userGroupId, userIds);

                return this.JsonNet(res);
            }
        }

        /// <summary>
        /// 反关联用户
        /// </summary>
        /// <param name="userGroupId">用户组Id</param>
        /// <param name="userIds">用户Id</param>
        /// <returns></returns>
        public ActionResult UnRelationUser(Guid userGroupId, Guid[] userIds)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.UnRelationUser(userGroupId, userIds);

                return this.JsonNet(res);
            }
        }
    }
}